package hotel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;

public class dateChooser  extends JFrame implements ActionListener
{
	private JFrame frame;
	private static final long serialVersionUID = 1L;
	private JPanel masterPanel = new JPanel();
	private JMenuBar masterMenuBar = new JMenuBar();
	protected JPopupMenu popup;
	private JMenu monthMenu;
	private JMenu dayMenu;
	private JMenu yearMenu;
	private Integer bookingDay;
	private Integer bookingMonth;
	private Integer bookingYear;
	private Integer currentDay;
	private Integer currentMonth;
	private Integer currentYear;
	private ArrayList<Integer> yearsToShow = new ArrayList<Integer>();
	private ArrayList<Integer> monthsToShow = new ArrayList<Integer>();
	private ArrayList<Integer> daysToShow = new ArrayList<Integer>();
	private userApplication userApp;
	private String type;
	
	
	public dateChooser(String type, userApplication userApp)
	{
		this.type = type;
		this.userApp = userApp;
		frame = new JFrame("Date Chooser: " + type);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		frame.setLocation((int) (screenSize.width * 0.35), (int) (screenSize.height * 0.35));
	
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setSize(new Dimension(400, 200));
		frame.setResizable(false);
		
		if(type.equals("checkIn"))
		{
			bookingYear   = userApp.checkinYear;
			bookingMonth = userApp.checkinMonth;
			bookingDay   =  userApp.checkinDay;
		}
		else
		{
			bookingYear  = userApp.checkoutYear;
			bookingMonth = userApp.checkoutMonth;
			bookingDay   =  userApp.checkoutDay;
		}
		
		currentYear = 2011;
		currentMonth = 5;
		currentDay =  1;
		
		masterPanel.setLayout(new BorderLayout());
		updateMenu();
		masterPanel.add(masterMenuBar, BorderLayout.NORTH);
		frame.add(masterPanel, BorderLayout.CENTER);
		frame.setVisible(true);	
	}

	private void updateMenu()
	{
		masterPanel.removeAll();
		masterMenuBar.removeAll();
		monthMenu = new JMenu("Month");
		monthMenu.addActionListener(this);
		dayMenu = new JMenu("Day");
		dayMenu.addActionListener(this);
		yearMenu = new JMenu("Year");
		yearMenu.addActionListener(this);
		findMonthsAndYears();
		masterMenuBar.add(monthMenu);
		masterMenuBar.add(dayMenu);
		masterMenuBar.add(yearMenu);
		masterPanel.add(masterMenuBar, BorderLayout.NORTH);
		JButton updateButton = new JButton();
		updateButton = new JButton("   Update  ");
		updateButton.setActionCommand("update");
		updateButton.addActionListener(this);
		masterPanel.add(updateButton, BorderLayout.SOUTH);
		frame.setVisible(true);
	}
	
	private void findMonthsAndYears()
	{
		yearsToShow = new ArrayList<Integer>();
		monthsToShow = new ArrayList<Integer>();
		daysToShow = new ArrayList<Integer>();
		Calendar c = Calendar.getInstance();
		
		int DayOfYear = c.get(Calendar.DAY_OF_YEAR);
		int numYears;
		
		//Determine years shown
		if(DayOfYear+31 > 365)
			numYears = 2;
		else
			numYears = 1;
		for(int i = 0; i< numYears; i++)
		{
			String year = "" + (currentYear + i);
			JMenuItem menuItem = new JMenuItem(year);
			menuItem.setActionCommand("Year"+year);
			menuItem.addActionListener(this);
			yearMenu.add(menuItem);
			yearsToShow.add(currentYear + i); 
		}
		if((bookingYear - currentYear) != 0)
		{
			JMenuItem menuItem = new JMenuItem(""  + 1);
			menuItem.setActionCommand("Month"+1);
			menuItem.addActionListener(this);
			monthMenu.add(menuItem);
			monthsToShow.add(1); 
		} else
		{
			JMenuItem menuItem = new JMenuItem("" + currentMonth);
			menuItem.setActionCommand("Month"+ currentMonth);
			menuItem.addActionListener(this);
			monthMenu.add(menuItem);
			//menuItem = new JMenuItem("" + (currentMonth+1));
			//menuItem.setActionCommand("Month"+ (currentMonth+1));
			//menuItem.addActionListener(this);
			//monthMenu.add(menuItem);
			monthsToShow.add(currentMonth); 
		} 
		int startingDay = currentDay;
		if((bookingMonth - currentMonth) != 0)
			startingDay = 1;
		int daysInMonth = 31;
		if((bookingMonth - 2) == 0)
			daysInMonth = 28;
		else if((bookingMonth-4) == 0  || (bookingMonth-6) == 0 || (bookingMonth-9) == 0 || (bookingMonth-11) == 0)
			daysInMonth = 30;
		for(int i =  startingDay; i<=daysInMonth; i++)
		{
			String day = "" + i;
			JMenuItem menuItem = new JMenuItem(day);
			menuItem.setActionCommand("Day"+i);
			menuItem.addActionListener(this);
			dayMenu.add(menuItem);
			daysToShow.add(i);
		}
		JTextArea dateArea = new JTextArea("Booked date: " + bookingMonth + "-" + bookingDay + "-" + bookingYear);
		dateArea.setEditable(false);
		dateArea.setHighlighter(null);
		masterPanel.add(dateArea, BorderLayout.CENTER );
	}

	public void actionPerformed(ActionEvent ex)
	{
		String s = ex.getActionCommand();
		if(s.contains("Year"))
		{
			
			s = s.replaceAll("Year", "");
			bookingYear = Integer.valueOf(s);
			bookingDay = currentDay;
			bookingMonth = currentMonth;
			System.out.println("bmonth" + bookingYear);
			System.out.println("cmonth" + currentYear);
			if((bookingYear - currentYear) != 0)
				bookingMonth = 1;
			updateMenu();
		} 
		else if(s.contains("Month"))
		{
			
			s = s.replaceAll("Month", "");
			bookingMonth = Integer.valueOf(s);
			bookingDay = currentDay;
			updateMenu();
		}
		else if(s.contains("Day"))
		{
			s = s.replaceAll("Day", "");
			bookingDay = Integer.valueOf(s);
			updateMenu();
		}
		else if(s.contains("update"))
		{
			userApp.update(bookingYear, bookingMonth, bookingDay, type);
		}
	}
}
